package com.reggie.module.controller;

import com.reggie.common.ResultResponseVo;
import com.reggie.common.enums.backend.DishEnum;
import com.reggie.module.entity.dto.dishDto.DishDto;
import com.reggie.module.service.CategoryService;
import com.reggie.module.service.DishFlavorService;
import com.reggie.module.service.DishService;
import com.reggie.module.service.DishStatusService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @Author 早睡蛋
 * @Date 2022/9/17 14:30:42
 * @Desc:
 */
@RestController
@Slf4j
@RequestMapping("/dish")
@CrossOrigin
@Validated
@Api(tags = "菜品管理模块")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private DishStatusService dishStatusService;


    /**
     * 添加菜品
     *
     * @param dishDto
     * @return
     */
    @PostMapping
    public ResultResponseVo save(@RequestBody DishDto dishDto, HttpServletRequest request) {
        return dishService.saveDish(dishDto, request);
    }

    /**
     * 菜品信息分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public ResultResponseVo page(int page, int pageSize, String name) {
        return dishService.selectByPage(page, pageSize, name);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResultResponseVo getById(@PathVariable Long id, HttpServletRequest request) {
        return dishService.getDishById(id, request);
    }

    /**
     * 修改菜品
     *
     * @param dishDto
     * @return
     */
    @PutMapping
    public ResultResponseVo updateDish(@RequestBody DishDto dishDto, HttpServletRequest request) {
        return dishService.updateDish(dishDto, request);
    }

    /**
     * 菜品 起售/停售
     *
     * @param status
     * @param ids
     * @param request
     * @return
     */
    @PostMapping("/status/{status}")
    public ResultResponseVo changeStatus(@PathVariable Integer status,
                                         @RequestParam(value = "ids") List<Long> ids,
                                         HttpServletRequest request) {
        log.info("status:{}", status);
        log.info("ids:{}", ids);
        int size = ids.size();
        if (size>10){
            return DishEnum.SIZE_MAX.getResultResponseVo();
        }
        return dishStatusService.changeStatus(status, ids, request);
    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    public ResultResponseVo deleteDish(@RequestParam(value = "ids") List<Long> ids){
        log.info("ids:{}", ids);
        int size = ids.size();
        if (size>10){
            return DishEnum.SIZE_MAX.getResultResponseVo();
        }
        return dishService.deleteDish(ids);
    }

    @GetMapping("/list")
    public ResultResponseVo listById(@RequestParam(value = "categoryId") Long categoryId,
                                     @RequestParam(value = "status",required = false) Integer status){
        log.info("categoryId:{},status:{}",categoryId,status);
        return dishService.getListById(categoryId,status);
    }


}
